//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.bfsuol.bfsuolcomponents.log;

import com.bfsuol.bfsuolcomponents.log.dao.SysLog;
import com.bfsuol.bfsuolcomponents.log.service.SysLogService;
import com.bfsuol.bfsuolframework.core.configuration.ConfigUtils;
import com.bfsuol.bfsuolframework.core.controller.LoginInfo;
import com.bfsuol.bfsuolframework.core.spring.SpringBeanLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;

public class BfsuolLogger {
    private static final BfsuolLogger instance = new BfsuolLogger();
    SysLogService logService = null;
    private boolean batchSave = true;
    private Queue<SysLog> queue = new ConcurrentLinkedQueue();

    private BfsuolLogger() {
        int maxPeriod = ConfigUtils.getInt("logger.MaxPeriod", 10);
        int maxCount = ConfigUtils.getInt("logger.MaxCount", 200);
        this.batchSave = ConfigUtils.getBoolean("sys.LoggerBatchSave", true);
        if(this.batchSave) {
            Timer timer = new Timer();
            timer.schedule(new LoggerSaveTask(this, maxPeriod, maxCount), 1000L, 1000L);
        } else {
            this.logService = (SysLogService)SpringBeanLoader.getBean("sysLogServiceImpl");
        }

    }

    public static void error(Object... content) {
        instance.log("错误", BfsuolLoggerLevel.ERROR, content);
    }

    public static void debug(Object... content) {
        instance.log("调试", BfsuolLoggerLevel.DEBUG, content);
    }

    public static void info(Object... content) {
        instance.log("业务操作", BfsuolLoggerLevel.INFO, content);
    }

    public static void data(Object... content) {
        instance.log("数据更新", BfsuolLoggerLevel.INFO, content);
    }

    private void log(String type, BfsuolLoggerLevel level, Object... content) {
        if(content != null && content.length > 0) {
            SysLog log = new SysLog();
            StringBuffer log_content = new StringBuffer();
            Object[] var9 = content;
            int var8 = content.length;

            for(int var7 = 0; var7 < var8; ++var7) {
                Object str = var9[var7];
                if(str != null) {
                    log_content.append(str);
                }
            }

            log.setContent(log_content.toString());
            log.setType(type);
            //$SWITCH_TABLE$com$bfsuolcomponents$log$BfsuolLogger$BfsuolLoggerLevel()
            switch(level.ordinal()) {
                case 1:
                    log.setLevel(Integer.valueOf(1));
                    break;
                case 2:
                    log.setLevel(Integer.valueOf(2));
                    break;
                case 3:
                    log.setLevel(Integer.valueOf(3));
            }

            log.setCreateDate(new Date());
            if(RequestContextHolder.getRequestAttributes() != null) {
                HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
                if(request != null) {
                    log.setServerName(request.getServerName());
                    log.setServerIp(request.getRemoteHost());
                    LoginInfo info = (LoginInfo)request.getSession().getAttribute("key_login_info_session");
                    if(info != null) {
                        log.setUserName(info.getUserName());
                        log.setUserId(info.getUserId());
                    } else {
                        log.setUserName("未登录");
                        log.setUserId(Long.valueOf(0L));
                    }

                    String userAgent = request.getHeader("user-agent");
                    log.setUserAgent(userAgent);
                    log.setLoginIp(this.getClientIp(request));
                }
            }

            if(this.batchSave) {
                this.queue.offer(log);
            } else if(this.logService != null) {
                this.logService.save(log);
            }

        }
    }

    private String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    public static enum BfsuolLoggerLevel {
        ERROR,
        DEBUG,
        INFO;

        private BfsuolLoggerLevel() {
        }
    }

    public class LoggerSaveTask extends TimerTask {
        private BfsuolLogger logger;
        private SysLogService loggerService;
        private int maxCount = ConfigUtils.getInt("sys.BATCH_SIZE", 20);
        private int maxPeriod = 1;
        private int period = 0;
        private int count = 0;
        private List<SysLog> logList = new LinkedList();

        public LoggerSaveTask(BfsuolLogger logger, int maxPeriod, int maxCount) {
            this.logger = logger;
            this.loggerService = (SysLogService)SpringBeanLoader.getBean("sysLogServiceImpl");
            this.maxPeriod = maxPeriod > 0?maxPeriod:ConfigUtils.getInt("sys.BATCH_SIZE", 20);
            this.maxCount = maxCount > 0?maxCount:1;
        }

        public void run() {
            if(this.logger != null && this.loggerService != null) {
                SysLog log;
                while((log = (SysLog)this.logger.queue.poll()) != null) {
                    this.logList.add(log);
                    ++this.count;
                }

                ++this.period;
                if(this.count > this.maxCount || this.period > this.maxPeriod) {
                    try {
                        if(this.count > 0) {
                            this.loggerService.BatchSave(this.logList);
                            this.logList = new LinkedList();
                        }
                    } catch (Exception var3) {
                        var3.printStackTrace();
                    }

                    this.count = 0;
                    this.period = 0;
                }

            }
        }
    }
}
